/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.gui;

import com.vasthu.api.domain.User;
import com.vasthu.api.service.UserManagerService;
import com.vasthu.core.service.UserManagerServiceImpl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author shan
 */
public class VstLoginForm extends JFrame {
    
    private JLabel lCaption;
    private JLabel lUsername;
    private JLabel lPassword;
    private JLabel lErrorField;
    private JTextField tUsername;
    private JPasswordField tPassword;
    private JButton okButton;
    private JButton cancelButton;
    private int maxAttempts;
    
    public VstLoginForm () {
        maxAttempts = 3;
        initUi();
    }

    private void initUi() {
        
        /* Initializing Controllers */
        lCaption = new JLabel("Login Form");
        lCaption.setFont(new Font("Monospaced 12", Font.BOLD, 16));
        lCaption.setHorizontalAlignment(SwingUtilities.CENTER);
        lCaption.setPreferredSize(new Dimension(290, 30));
        lUsername = new JLabel("Username : ");
        lUsername.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        lUsername.setHorizontalAlignment(SwingUtilities.RIGHT);
        lUsername.setPreferredSize(new Dimension(85, 25));
        lPassword = new JLabel("Password : ");
        lPassword.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        lPassword.setHorizontalAlignment(SwingUtilities.RIGHT);
        lPassword.setPreferredSize(new Dimension(85, 25));
        lErrorField = new JLabel("");
        lErrorField.setForeground(Color.RED);
        lErrorField.setFont(new Font("Monospaced 12", Font.BOLD, 12));
        lErrorField.setHorizontalAlignment(SwingUtilities.CENTER);
        lErrorField.setPreferredSize(new Dimension(290, 25));
        tUsername = new JTextField();
        tUsername.setPreferredSize(new Dimension(200, 25));
        tPassword = new JPasswordField();
        tPassword.setPreferredSize(new Dimension(200, 25));
        okButton = new JButton(" LogIn ");
        okButton.setPreferredSize(new Dimension(145, 25));        
        cancelButton = new JButton(" Cancel ");
        cancelButton.setPreferredSize(new Dimension(145, 25));    
        
        /* Enter Key Press Order: tUsername -> tPassword -> okButton */
        tUsername.requestFocus();
        tUsername.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                /*if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    tPassword.requestFocus();
                }*/
            }

            @Override
            public void keyPressed(KeyEvent e) {
                /*if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    tPassword.requestFocus();
                }*/
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    tPassword.requestFocus();
                }
            }
        });
        
        tPassword.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                /*if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    okButton.requestFocus();
                }*/
            }

            @Override
            public void keyPressed(KeyEvent e) {
                /*if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    okButton.requestFocus();
                }*/
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    okButton.requestFocus();
                }
            }
        });
        
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                UserManagerService userManager = new UserManagerServiceImpl();
                
                String username = tUsername.getText();
                String password = new String(tPassword.getPassword());
                User validUser = userManager.authenticate(username, password);

                if (validUser != null) {
                    VstMainWindow mainForm = new VstMainWindow();
                    mainForm.setVisible(true);
                    setVisible(false);
                } else {
                    --maxAttempts;
                    tUsername.setText("");
                    tPassword.setText("");
                }
                
                if (maxAttempts == 0) {
                    JOptionPane.showMessageDialog(null, "Please contact your Admin", "Info", JOptionPane.ERROR_MESSAGE);
                    System.exit(0);
                } else if (validUser == null) {
                    JOptionPane.showMessageDialog(null, "ACCESS DENIED!\nTry Again", "Warn", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });      
        
        JPanel basePanel = new JPanel();
        basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(lCaption);
        basePanel.add(topPanel);
        
        JPanel unPanel = new JPanel(new FlowLayout());
        unPanel.add(lUsername);
        unPanel.add(tUsername);
        basePanel.add(unPanel);
        JPanel pwPanel = new JPanel(new FlowLayout());
        pwPanel.add(lPassword);
        pwPanel.add(tPassword);
        basePanel.add(pwPanel);
        
        basePanel.add(lErrorField);
        
        JPanel bottomPanel = new JPanel(new FlowLayout());
        bottomPanel.add(okButton);
        bottomPanel.add(cancelButton);
        basePanel.add(bottomPanel);
        
        add(basePanel);
        
        java.net.URL imgUrl = VstLoginForm.class.getResource("../images/app_icon.gif");
        
        if (imgUrl != null) {
            setIconImage(new ImageIcon(imgUrl).getImage());
        }

        setMinimumSize(new Dimension(320, 170));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 330) / 2, (screen.height - 200) / 2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(false);
    }
    
}
